var AutoInputMethod = InputMethod.extend({
	imlist: null,
	target_list: 'Num',
	current_list: null,
	constructor: function()
	{
	},
	IMName: function()
	{
		return $GFS('Auto') + '(experimental)';
	},
	GetLanguage: function()
	{
		return 'auto';
	},
	GetMenuNode: function()
	{
		return null;
	},
	onActive: function()
	{
		this.UpdateIMList();
	},
	onDeactive: function()
	{
		this.imlist = null;
	},
	UpdateIMList: function()
	{
		var ta = this.ime.type_array;
		var i;

		this.imlist = new Array();

		// only get LOCAL IM now
		for(i=0;i<ta.length;i++) {
			if(ta[i] == GownFull.LOCAL) {
				var im = this.ime.im_array[i];

				if(im.onStartKeystroke(null)) {
					var o = {};
		
					o.im = im;
					o.data = {};
					this.imlist.push(o);

					im.onStartKeystroke(o.data);
					Debug.trace(0,'AutoInputMethod: get input method %d (%s)',i,o.im.IMName());
				}
			}
		}
	},
	ResetAll: function()
	{
		if(this.imlist) {
			for(i=0;i<this.imlist.length;i++) {
				var im = this.imlist[i].im;

				im.onEndKeystroke(this.imlist[i].data);
				this.imlist[i].data = {};
				im.onStartKeystroke(this.imlist[i].data);
			}
		}
		this.current_list = null;
		this.ime.ResetPreedit();
		this.UpdateCandidate();
	},
	onCancel: function(obj)
	{
		this.ResetAll();
		Debug.trace(0,'AutoInputMethod: cancel!');
	},
	ListSelectHandler: function(obj,pos)
	{
		if(this.current_list[pos].html) this.ime.SendHTML(obj,this.current_list[pos].html);
		else if(this.current_list[pos].string) this.ime.SendString(obj,this.current_list[pos].string);
		this.ResetAll();
		return true;
	},
	UpdateCandidate: function()
	{
		if(this.current_list) {
			var list = new Array();
			var i;
			var x = this;

			for(i=0;i<this.current_list.length;i++) {
				list[i] = this.current_list[i].display;
			}

			if(this.ime.candwin.hasList(this.target_list)) this.ime.candwin.UpdateList(this.target_list,list);
			else {
				this.ime.candwin.ResetList(this.target_list);
				this.ime.candwin.SetList(null,list,function(obj,pos) { return x.ListSelectHandler(obj,pos); },this.target_list);
			}
			this.ime.candwin.SetStatus(null);
			this.ime.candwin.SetCancelHandler(function() { x.onCancel(null); });
			this.ime.candwin.UpdateCandidate();
		}
		else {
			this.ime.candwin.SetStatus(null);
			this.ime.candwin.ResetList(this.target_list);
			this.ime.candwin.UpdateCandidate();
		}
	},
	UpdateList: function()
	{
		// build the list...
		var i,j,imdata;

		if(this.imlist) {
			this.current_list = new Array();

			for(i=0;i<this.imlist.length;i++) {
				imdata = this.imlist[i];
				if(imdata.data.result) {
					for(j=0;j<imdata.data.result.length;j++) {
						var o = imdata.data.result[j];
						var no;

						no.display = o.display;
						no.string = o.string;
						no.html = o.html;
						no.display = '[' + imdata.im.IMName() + '] ' + no.display;
						this.current_list.push(no);
					}
				}
				Debug.trace(0,'AutoInputMethod: id=%d, preedit=%s',i,imdata.data.preedit);
			}

			Debug.trace(0,'AutoInputMethod: generated list %d candidate(s) available.',this.current_list.length);
			if(this.current_list.length == 0) this.current_list = null;
		}
		else this.current_list = null;
		this.UpdateCandidate();
	},
	keydown_handler: function(obj,code,modifiers)
	{
		if(!this.imlist) return true;

		var b = true;
		var i;
		var key;
		var imdata;

		key = {};
		key.code = code;
		key.mod = modifiers;
		key.type = Const.KEY_TYPE_DOWN;
		this.ime.AppendPreedit('',lib.sprintf('%dD%b ',code,modifiers));
		for(i=0;i<this.imlist.length;i++) {
			imdata = this.imlist[i];

			if(!imdata.im.evalKeystroke(key,imdata.data)) b = false;
		}
		if(!b) this.UpdateList();

		return b;
	},
	keyup_handler: function(obj,code,modifiers)
	{
		if(!this.imlist) return true;

		var b = true;
		var i;
		var key;
		var imdata;

		key = {};
		key.code = code;
		key.mod = modifiers;
		key.type = Const.KEY_TYPE_UP;
		this.ime.AppendPreedit('',lib.sprintf('%dU%b ',code,modifiers));
		for(i=0;i<this.imlist.length;i++) {
			imdata = this.imlist[i];

			if(!imdata.im.evalKeystroke(key,imdata.data)) b = false;
		}
		if(!b) this.UpdateList();

		return b;
	},
	keypress_handler: function(obj,code,modifiers)
	{
		if(!this.imlist) return true;

		if(code == 27 && modifiers == 0) { // Escape
			this.ResetAll();
			return false;
		}
		var b = true;
		var i;
		var key;
		var imdata;

		key = {};
		key.code = code;
		key.mod = modifiers;
		key.type = Const.KEY_TYPE_PRESS;
		this.ime.AppendPreedit('',lib.sprintf('%dP%b ',code,modifiers));
		for(i=0;i<this.imlist.length;i++) {
			imdata = this.imlist[i];

			if(!imdata.im.evalKeystroke(key,imdata.data)) b = false;
		}
		if(!b) this.UpdateList();

		return b;
	}
});
